<template>
    <van-action-sheet v-model:show="visible" title="请选择处理方式" :closeable="false" cancel-text="取消" close-on-click-action z-index="999999">
        <div class="content" v-for="item in buttonList" :key="item.name" @click="btnClick(item)">{{
            BTN_TYPE[item.name]?.title }}</div>
        <div class="content" v-if="messageEnum.includes(currentState) && messageShow" @click="messageClick">留言</div>
    </van-action-sheet>

    <!-- 按钮新增 -->
    <ButtonAddForm v-model="addShow" ref="btnRef" :status-type="statusType" :btn-type="btnType"
        :ticket-info="ticketInfo" @success="success" />

    <!-- 修改提交，确认提交 -->
    <CommitForm v-model="commitShow" :commit-data="commitData" :type-number="typeNumber" :fab-id="fabId"
        @success="success" />
</template>

<script setup lang='ts'>
import { BTN_TYPE } from '@/enum/clientType'
import ButtonAddForm from './ButtonAddForm.vue'
import CommitForm from './CommitForm.vue'
import { ButtonType, SubMitType, TicketInfoType } from '@/api/worksheets/type';

const prop = defineProps<{
    buttonList: ButtonType[]
    currentState: any//当前状态
    messageShow: boolean//留言按钮是否显示
    ticketInfo: TicketInfoType//工单信息
    subMitData: SubMitType//修改提交，确认提交数据
    fabId: any//设备id
}>()

const emit = defineEmits<{
    (e: 'btnSuccess'): void
}>()

const visible = defineModel<boolean>('modelValue')

const messageEnum = [1, 2, 3, 4]

const addShow = ref(false)

const commitShow = ref(false)

const typeNumber = ref('')

const statusType = ref(0)//0-按钮新增 1-留言

const btnType = ref<ButtonType>({} as ButtonType)

const commitData = ref<SubMitType>({} as SubMitType)

const btnRef = ref<InstanceType<typeof ButtonAddForm>>()

//按钮点击
const btnClick = async (item: ButtonType) => {

    btnRef.value.clearForm()

    btnType.value = item

    statusType.value = 0

    if (btnType.value.name === 'commit_ticket' || btnType.value.name === 'resubmit_ticket') {
        //确认提交cc
        const resubmit: any = prop.buttonList.find((item: any) => item.name === 'resubmit_ticket')?.init_data
            .cc

        //修改提交cc
        const editCc: any = prop.buttonList.find((item: any) => item.name === 'commit_ticket')?.init_data
            .cc
        commitData.value = {
            ...prop.subMitData,
            cc_users_list: btnType.value.name === 'resubmit_ticket' ? resubmit : editCc, //CC
        }
        if (btnType.value.name === 'commit_ticket') {
            typeNumber.value = '1'
        } else {
            typeNumber.value = '2'
        }

        commitShow.value = true
    } else {
        addShow.value = true
    }

    visible.value = false
}

//留言按钮
const messageClick = () => {

    btnRef.value.clearForm()

    statusType.value = 1

    visible.value = false

    addShow.value = true
}

//按钮新增保存
const success = () => {
    emit('btnSuccess')
}

</script>

<style lang="scss" scoped>
.content {
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
    border-bottom: 1px solid #eee;
}
</style>